import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DatePipe } from '@angular/common';
import { agentApplyStateArrs } from '../../cooperation-manage.pipe';
import { AdvertisingAgencyManageService } from '../advertising-agency-manage.service';

@Component({
  selector: 'app-advertising-agency-apply',
  templateUrl: './advertising-agency-apply.component.html',
  styleUrls: ['./advertising-agency-apply.component.scss'],
  providers: [DatePipe]
})
export class AdvertisingAgencyApplyComponent implements OnInit {

  searchParams: any = {
    applicationBeginTime: '',
    applicationEndTime: '',
    statusList: [],
    appName: '',
    companyName: ''
  };
  stateOptions: any = [...agentApplyStateArrs];
  applyDates: any = [];

  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  tableData: any = [];

  // 审核Modal
  isExamineVisible: boolean = false;
  examineItem: any = {};
  auditOpinion: string = '';

  constructor(
    private message: NzMessageService,
    private datePipe: DatePipe,
    private services: AdvertisingAgencyManageService
  ) { }

  ngOnInit(): void {
    this.loadTable()
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.tableLoading = true;
    const res: any = await this.services.agencyApplicationList(params);
    this.tableParams.tableLoading = false;
    
    if (res.code == 0) {
      const { records, total } = res.data || {};
      this.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.tableData = [];
      this.tableParams.total = 0;
      this.message.error(res.message);
    }
  };

  searchData() {
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData() {
    for (let key in this.searchParams) {
      if (Array.isArray(this.searchParams[key])) {
        this.searchParams[key] = [];
      } else {
        this.searchParams[key] = '';
      }
    }
    this.applyDates = [];
    this.tableParams.page = 1;
    this.loadTable();
  }

  timeChange(result: any) {
    this.searchParams.applicationBeginTime = result.length ? this.datePipe.transform(result[0], 'yyyy-MM-dd') : '';
    this.searchParams.applicationEndTime = result.length ? this.datePipe.transform(result[1], 'yyyy-MM-dd') : '';
  }

  showExamineModal(item: any) {
    this.examineItem = item;
    this.auditOpinion = '';
    this.isExamineVisible = true;
  }
  handleCancel() {
    this.isExamineVisible = false;
  }
  async examineSubmit(type: number) {
    if (type === 2 && this.auditOpinion == '') {
      this.message.warning('请先输入审核意见');
      return
    }
    let params = {
      id: this.examineItem.id,
      status: type,
      auditOpinion: this.auditOpinion
    }
    
    const res: any = await this.services.agentAuditItem(params);
    if (res.code == 0) {
      this.isExamineVisible = false;
      this.message.success('操作成功');
      this.loadTable();
    } else {
      this.message.error(res.message);
    }
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // 页数改变时的回调函数
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }
}
